const CONFIG = require('../../config.js');
// 文案匹配
let TEXTData = {};
Page({
	data: {
		navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#488dea))',
		loginInfo: null,
		planList: [],
		dataList: [],
		isTrueData: '1',
		pageNum: 1,
		pageSize: 6,
		isEmpty: false,
		textJsonData: []
	},
	// 方案管理信息列表
	getPlanData(){
		wx.request({
			url: CONFIG.host + "/system/matchInfoController/getMatchInfoListForAPP",
			data: {
				pageNum: this.data.pageNum,
				pageSize: this.data.pageSize,
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 200){
					if(res.data.rows.length === 0){
						wx.showToast({
							title: '没有更多了',
							icon: 'none',
							duration: 2000
						});
						this.setData({
							isEmpty: true
						})
						return;
					}
					if(this.data.pageNum > 1){
						this.data.planList = this.data.planList.concat(res.data.rows);
					}else{
						this.data.planList = res.data.rows;
					}
					this.setData({
						planList: this.data.planList,
						isEmpty: false
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 解锁提示
	unlockFn(e){
		let { id } = e.currentTarget.dataset;
		let that = this;
		wx.showModal({
			title: '提示',
			content: '解锁将消耗100积分,确定要解锁吗？',
			success: function (sm) {
			  	if (sm.confirm) {
					that.setUnlock(id);
				} else if (sm.cancel) {
				  	console.log('用户点击取消')
				}
			}
		})
	},

	// 解锁方案管理信息
	setUnlock(id){
		wx.request({
			url: CONFIG.host + "/system/matchInfoController/unlockingMatchInfoForAPP",
			data: {
				id: id
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				wx.showToast({
					title: res.data.message,
					icon: 'none',
					duration: 2000
				})
				this.getPlanData();
			},fail: function (error) {
				console.log(error);
			}
		})
	},
	
	// 查看详情
	showDetailFn(e){
		let { id,status } = e.currentTarget.dataset;
		if(status === 0) return;
		wx.navigateTo({
			url: '/pages/planSolution/index?reportId=' + id,
		})
	},
	
	// 获取列表数据
	getShowData(){
		wx.request({
			url: CONFIG.host + "/system/appTestController/getConsultationList",
			data: { 
				pageNum: this.data.pageNum,
				pageSize: 6
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.rows.length === 0){
					wx.showToast({
						title: '没有更多了',
						icon: 'none',
						duration: 2000
					});
					this.setData({
						isEmpty: true
					})
					return;
				}
				this.setData({
					isEmpty: false
				})
				for(let i=0; i < res.data.rows.length; i++){
					if(res.data.rows[i].consultationType === '1'){
						res.data.rows[i].consultationName = '房产类咨询';
					}else if(res.data.rows[i].consultationType === '2'){
						res.data.rows[i].consultationName = '发票税务类咨询';
					}else if(res.data.rows[i].consultationType === '3'){
						res.data.rows[i].consultationName = '企业类咨询';
					}else if(res.data.rows[i].consultationType === '4'){
						res.data.rows[i].consultationName = '车辆类咨询';
					}
				}
				if(this.data.pageNum > 1){
					this.data.dataList = this.data.dataList.concat(res.data.rows);
				}else{
					this.data.dataList = res.data.rows;
				}
				this.setData({
					dataList:  this.data.dataList
				})
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	onLoad: function () {
		let showNum = wx.getStorageSync('showUser') === '0' ? '0' : '1';
		TEXTData = require('../../utils/text'+ showNum +'.js');
		let loginInfo = wx.getStorageSync('loginInfo') || '';
		if(loginInfo){
			this.setData({
				loginInfo : JSON.parse(loginInfo),
				textJsonData: TEXTData.textJson
			})
		}
		this.setData({
			isTrueData: wx.getStorageSync('showUser'),
		})
	},
	onShow: function () {
		this.setData({
			pageNum: 1
		})
		if(this.data.isTrueData === '1'){
			this.getShowData();
		}else{
			this.getPlanData();
		}
	},
	/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () {
		wx.showNavigationBarLoading();
		this.setData({
			pageNum: 1
		})
		if(this.data.isTrueData === '1'){
			this.getShowData();
		}else{
			this.getPlanData();
		}
	},
	 /**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function () {
		if(this.data.isEmpty) return;
		this.setData({
			pageNum: this.data.pageNum + 1
		})
		if(this.data.isTrueData === '1'){
			this.getShowData();
		}else{
			this.getPlanData();
		}
	},
})
